package com.yonyou.wdt.utils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

public class DateUtils {

    //===============================================指定天数（可提前或延后）同步一次数据获取时间===========================================================

    /**
     * @param day 正数为未来几天，负数为过去几天
     * @return String
     * @Authro: Huang
     * @Description: 查询当前日期的前几天开始时间
     * @Date: 2022/11/5 10:58
     */
    public static String getStartDate(int day) {
        Calendar zero = Calendar.getInstance();
        zero.add(Calendar.DATE, day);
        zero.set(zero.get(Calendar.YEAR), zero.get(Calendar.MONTH), zero.get(Calendar.DATE), 0, 0, 0);
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dft.format(zero.getTime());
    }

    /**
     * @param day 正数为未来几天，负数为过去几天
     * @return String
     * @Authro: Huang
     * @Description: 查询当前日期的前几天结束时间
     * @Date: 2022/11/5 10:58
     */
    public static String getEndDate(int day) {
        Calendar zero = Calendar.getInstance();
        zero.add(Calendar.DATE, day);
        zero.set(zero.get(Calendar.YEAR), zero.get(Calendar.MONTH), zero.get(Calendar.DATE), 23, 59, 59);
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dft.format(zero.getTime());
    }

    //===============================================一天同步一次数据获取时间===========================================================

    public static String getStartDate() {
        Calendar zero = Calendar.getInstance();
        zero.add(Calendar.DATE, -1);
        zero.set(zero.get(Calendar.YEAR), zero.get(Calendar.MONTH), zero.get(Calendar.DATE), 0, 0, 0);
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dft.format(zero.getTime());
    }

    public static String getEndDate() {
        Calendar zero = Calendar.getInstance();
        zero.add(Calendar.DATE, -1);
        zero.set(zero.get(Calendar.YEAR), zero.get(Calendar.MONTH), zero.get(Calendar.DATE), 23, 59, 59);
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dft.format(zero.getTime());
    }

    //===============================================10分钟同步一次数据获取时间===========================================================

    /**
     * 开始时间（10分钟前）
     *
     * @return
     */
    public static String getStartDateBy10m() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MINUTE, -10);
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dft.format(calendar.getTime());
    }

    /**
     * 结束时间（系统当前时间）
     *
     * @return
     */
    public static String getEndDateBy10m() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }

    //================================================60分钟同步一次数据获取时间==========================================================

    public static String getStartDateBy30m() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MINUTE, -30);
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dft.format(calendar.getTime());
    }

    /**
     * 结束时间（系统当前时间）
     *
     * @return
     */
    public static String getEndDateBy30m() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }

    // ================================================START=================================================================
    public static TreeMap<String, String> getDateTime(Map<String, Object> map) {
        TreeMap<String, String> treeMap = new TreeMap<String, String>();
        String end = map.get("End").toString();
        String start = map.get("Start").toString();
        int hours = getHours(start, end);
        for (int i = 1; i <= hours; i++) {
            String dateTime = getDateTime(start, i * 60 * 60 - 1);
            String startTime = i == 1 ? start : getDateTime(start, (i - 1) * 60 * 60);
            if (getDateTime(dateTime).before(getDateTime(end))) {
                treeMap.put(startTime, dateTime);
            } else {
                treeMap.put(startTime, end);
            }
        }
        return treeMap;
    }

    private static int getHours(String startDateTime, String endDateTime) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date parse1 = dateFormat.parse(startDateTime);
            Date parse2 = dateFormat.parse(endDateTime);
            long mills = parse2.getTime() - parse1.getTime();
            //转 BigDecimal
            BigDecimal fz = new BigDecimal(mills);
            //转 BigDecimal
            BigDecimal fm = new BigDecimal(60 * 60 * 1000);
            BigDecimal bwtHours = fz.divide(fm, 0, BigDecimal.ROUND_CEILING);
            return bwtHours.intValue();
        } catch (Exception e) {
            return 0;
        }
    }

    public static String getDateTime(String datestr, int seconds) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String resultTimeStr = "";
        try {
            Date currentTime = formatter.parse(datestr);


            Calendar calendar = Calendar.getInstance();
            calendar.setTime(currentTime);
            if (seconds > 0)
                calendar.add(Calendar.SECOND, seconds);
            resultTimeStr = formatter.format(calendar.getTime());
        } catch (Exception e) {

        }
        return resultTimeStr;
    }

    public static Date getDateTime(String datestr) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date resultTimeStr = null;
        try {
            Date currentTime = formatter.parse(datestr);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(currentTime);
            resultTimeStr = calendar.getTime();
        } catch (Exception e) {
        }
        return resultTimeStr;
    }

    // ===================================================END==============================================================
    public static String getStartDateBy60m() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MINUTE, -60);
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dft.format(calendar.getTime());
    }

    /**
     * 结束时间（系统当前时间）
     *
     * @return
     */
    public static String getEndDateBy60m() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }

}
